<template>
    <el-dialog v-model="dialogVisible" width="600" align-center @close="dialogClose" title="选择下面AI助手开始对话">
        <el-menu router>
            <el-scrollbar height="400">
                <el-menu-item v-for="item in modelList" @click="handleRobotClick(item)">
                    <RobotInfo :model="item" />
                </el-menu-item>
            </el-scrollbar>
        </el-menu>
    </el-dialog>
</template>

<script setup>
import { computed, ref, onMounted } from "vue"
import RobotInfo from "@/components/robots/RobotInfo.vue"
import { getModelList } from "@/api/model"
import { useAuthStore } from "@/stores/auth";
import { useSessionStore } from "@/stores/session"
import { useSessionsStore } from "@/stores/sessions";
import { useRoute, useRouter } from 'vue-router'
import { addSession } from "@/api/session";

const authStore = useAuthStore()
const sessionStore = useSessionStore()
const sessionsStore = useSessionsStore()
const modelList = ref([])
const router = useRouter()
const props = defineProps(['dialogVisible'])
const emit = defineEmits(['dialogClose'])
// const emit = defineEmits(['newSession'])

const dialogVisible = computed({
    get() {
        return props.dialogVisible
    },
})

const dialogClose = () => {
    emit('dialogClose')
}

onMounted(() => {
    //获取机器人列表
    if (authStore.isLoggedIn) {
        getRobotList()
    }
})


const getRobotList = async () => {
    getModelList().then(res => {
        modelList.value = res.data
    })
}

//新建会话
const handleRobotClick = (model) => {
    let session = {}
    addSession({
        uid: authStore.user.id,
        mid: model.id,
        content: "你好，我是" + model.modelname + "有什么可以帮助你的吗？"
    }).then(res => {
        session = res.data
        router.push({
            path: `/session/${session.id}`,
            query: {
                mid: session.mid,
                modelName: session.modelName
            }
        })
        //设置当前会话
        sessionStore.setSession(session)
        //创建新会话修改sessionsStore
        sessionsStore.createSession(session)
    })
    dialogClose()
}
</script>

<style scoped lang="scss"></style>